import cgi
import os
import time

from google.appengine.api import users
from google.appengine.api import mail
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
from google.appengine.ext.webapp import template


class OilPrice(db.Model):
			author = db.UserProperty()
			price = db.StringProperty()
			date = db.DateTimeProperty(auto_now_add=True)

class Pricing(webapp.RequestHandler):
		def post(self):
			oil_price = OilPrice()
			
			if users.get_current_user():
					oil_price.author = users.get_current_user()
				
			oil_price.price = self.request.get('price')
			oil_price.put()
			self.redirect('/admin/')

class Inquiry(db.Model):
			author = db.StringProperty()
			author_email = db.EmailProperty()
			description = db.StringProperty(multiline=True)
			date = db.DateTimeProperty(auto_now_add=True)
			company = db.StringProperty()
			phone = db.StringProperty()
			is_open = db.BooleanProperty()

class InquiryThanks(webapp.RequestHandler):
		def get(self):
			oil_price_query = OilPrice.all().order('-date')
			oil_prices = oil_price_query.fetch(1)
			
			if users.is_current_user_admin():
				admin_link = '| <a href="/admin/">Admin</a>'
			else:
				admin_link = ''

			if users.get_current_user():
				url = users.create_logout_url(self.request.uri)
				url_linktext = 'Logout'
			else:
				url = users.create_login_url(self.request.uri)
				url_linktext = 'Login'

			template_values = { 
				'page_title': "Inquiries",
				'site_name': "Ace Oil, Inc.",
				'oil_prices': oil_prices,
				'url': url,
				'url_linktext': url_linktext,
				'admin_link': admin_link
			}

			path = os.path.join(os.path.dirname(__file__), 'inquiry-thanks.html')
			self.response.out.write(template.render(path, template_values))

class Subscribed(webapp.RequestHandler):
		def get(self):
			oil_price_query = OilPrice.all().order('-date')
			oil_prices = oil_price_query.fetch(1)
			
			if users.is_current_user_admin():
				admin_link = '| <a href="/admin/">Admin</a>'
			else:
				admin_link = ''

			if users.get_current_user():
				url = users.create_logout_url(self.request.uri)
				url_linktext = 'Logout'
			else:
				url = users.create_login_url(self.request.uri)
				url_linktext = 'Login'

			template_values = { 
				'page_title': "Subscribed",
				'site_name': "Ace Oil, Inc.",
				'oil_prices': oil_prices,
				'url': url,
				'url_linktext': url_linktext,
				'admin_link': admin_link
			}

			path = os.path.join(os.path.dirname(__file__), 'subscribed.html')
			self.response.out.write(template.render(path, template_values))

class Inquiries(webapp.RequestHandler):
		def post(self):
			inquiry = Inquiry()
			inquiry.author = self.request.get('name')
			inquiry.author_email = self.request.get('email')
			inquiry.company = self.request.get('company')
			inquiry.description = self.request.get('description')
			inquiry.phone = self.request.get('phone')
			inquiry.is_open = True
		
			inquiry.put()
			
			mail.send_mail(sender="Ace Oil Inc <info@aceoilinc.com>",
			reply_to=self.request.get('email'),
			subject="New Ace Oil Customer Inquiry",
			to="webleads@aceoilinc.com",
			body=self.request.get('description') + "\n\n Phone: " + self.request.get('phone') + "\n Email: " + self.request.get('email') \
														 + "\n\n Name: " + self.request.get('name') + "\n Company: " + self.request.get('company'))
			self.redirect("/inquiry-thanks")
			
class MainPage(webapp.RequestHandler):
		def get(self):
			oil_price_query = OilPrice.all().order('-date')
			oil_prices = oil_price_query.fetch(1)
			
			if users.is_current_user_admin():
				admin_link = '| <a href="/admin/">Admin</a>'
			else:
				admin_link = ''

			if users.get_current_user():
				url = users.create_logout_url(self.request.uri)
				url_linktext = 'Logout'
			else:
				url = users.create_login_url(self.request.uri)
				url_linktext = 'Login'

			template_values = { 
				'page_title': "Home",
				'site_name': "Ace Oil, Inc.",
				'oil_prices': oil_prices,
				'url': url,
				'url_linktext': url_linktext,
				'admin_link': admin_link
			}

			path = os.path.join(os.path.dirname(__file__), 'index.html')
			self.response.out.write(template.render(path, template_values))

class PrePage(webapp.RequestHandler):
    def get(self):


			template_values = { 
				'page_title': "Home",
				'site_name': "Ace Oil, Inc."
			}

			path = os.path.join(os.path.dirname(__file__), 'index-pre.html')
			self.response.out.write(template.render(path, template_values))

														 				
class ContactPage(webapp.RequestHandler):
    def get(self):

			if users.is_current_user_admin():
				admin_link = '| <a href="/admin/">Admin</a>'
			else:
				admin_link = ''

			if users.get_current_user():
				url = users.create_logout_url(self.request.uri)
				url_linktext = 'Logout'
			else:
				url = users.create_login_url(self.request.uri)
				url_linktext = 'Login'

			template_values = { 
				'page_title': "Contact",
				'site_name': "Ace Oil, Inc.",
				'url': url,
				'url_linktext': url_linktext,
				'admin_link': admin_link
			}

			path = os.path.join(os.path.dirname(__file__), 'contact.html')
			self.response.out.write(template.render(path, template_values))


application = webapp.WSGIApplication(
                                     [('/', MainPage),
																		 ('/pricing', Pricing),
																		 ('/subscribed', Subscribed),
																		 ('/inquiry', Inquiries),
																		 ('/inquiry-thanks', InquiryThanks),
																		 ('/contact', ContactPage)],
                                     debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()